#!/bin/bash

MOD_DIR="openvice"

find_obj_impl()
{
  for i in `ls $MOD_DIR/*.ide`
  do
    awk -F', *' '{if($2=="'$1'") {print $1} }' $i
  done
}

find_obj()
{
  find_obj_impl $* | head -n 1
}

find_model_impl()
{
  for i in `ls vc/*.ide`
  do
    awk -F', *' '{if($1=="'$1'") {print $2} }' $i
  done
}

find_model()
{
  find_model_impl $* | head -n 1
}

total=0
converted=0

while read line
do
  if test `echo $line | awk -F', *' '{print NF}'` -ne 13
  then
    echo $line
    continue
  fi
  let total++

  #modelnamevc=`echo $line | awk -F', ' '{print $2}'`
  modelidvc=`echo $line | awk -F', *' '{print $1}'`
  
  modelname=`find_model $modelidvc`
  modelid=`find_obj $modelname`
  if test "$modelid" = ""; 
  then 
    echo "[$modelidvc] $modelname: not found!" > /dev/stderr
    continue
  fi

  interior=`echo $line | awk -F', *' '{print $3}'`

  posx=`echo $line | awk -F', *' '{print $4}'`
  posy=`echo $line | awk -F', *' '{print $5}'`
  posz=`echo $line | awk -F', *' '{print $6}'`

  rotx=`echo $line | awk -F', *' '{print $10}'`
  roty=`echo $line | awk -F', *' '{print $11}'`
  rotz=`echo $line | awk -F', *' '{print $12}'`
  rotw=`echo $line | awk -F', *' '{print $13}' | tr -d '\n\r\f\v\t'`


  echo "$modelid, $modelname, $interior, $posx, $posy, $posz, $rotx, $roty, $rotz, $rotw, -1"
  let converted++
done

echo "converted $converted/$total objects" > /dev/stderr
